<?php
namespace app\index\controller;

use app\index\model\Archives;
use app\index\model\ArchivesCategory;
use think\Request;
use app\index\model\Borrow;

class Borrows extends Common
{
    protected $request;
    protected $borrow_model;
    protected $a_model;
    protected $ac_model;
    public function __construct(Request $request = null)
    {
        parent::__construct($request);
        $this->request = $request;
        $this->borrow_model = new Borrow();
        $this->a_model = new Archives();
        $this->ac_model = new ArchivesCategory();
    }

    // 借阅
    public function index()
    {
        $params = $this->request->param();
        $condition = array();
        if(!empty($params['type'])){
            $condition['bor_status'] = $params['type'];
        }
        $in_data = $this->borrow_model->getListByCondition($condition,'bor_id desc');
        $new_data = array();
        if($in_data){
            foreach ($in_data as $key=>$val){
                $arc_data = $this->a_model->getDataByCondition(array('arc_id'=>$val['arc_id']));
                if(!empty($arc_data)) {
                    $cate_data = $this->ac_model->getDataByCondition(array('cate_id' => $arc_data['category_id']));
                    if(!empty($cate_data)) {
                        if ($arc_data) {
                            $new_data[$key] = $val;
                            $new_data[$key]['arc_name'] = $arc_data['arc_name'];
                            $new_data[$key]['arc_type'] = $arc_data['arc_type'];
                            $new_data[$key]['arc_file_size'] = $arc_data['arc_file_size'];
                            $new_data[$key]['category_id'] = $arc_data['category_id'];
                            $new_data[$key]['cate_type'] = $cate_data['cate_type'];
                            $new_data[$key]['audit_status'] = $arc_data['audit_status'];
                        }
                    }
                }
            }
        }
        $q_num = $this->borrow_model->getCountByCondition(array());
        $sq_num = $this->borrow_model->getCountByCondition(array('bor_status'=>1));
        $sh_num = $this->borrow_model->getCountByCondition(array('bor_status'=>2));
        $dh_num = $this->borrow_model->getCountByCondition(array('bor_status'=>3));

        $uid = $_SESSION['think']['username'][0]['user_id'];
        $time = date('Y-m-d H:i:s',time());
        $prev = Db('_user_log')
                ->where('user_id',$uid)
                ->order('create_time desc')
                ->limit(0,1)
                ->field('log_desc')
                ->find();
        $prevdata = $prev['log_desc'];

        if ($prevdata !== '借阅') {
            //记录用户操作
            $data = [
                'user_id' => $uid,  
                'log_action' => 'Borrow/index',   
                'log_desc' => '借阅',   //默认空
                'create_time' => $time,
                'is_delete' => 0 //默认0
            ];
            $res = db('_user_log')
                ->insert($data);
        }

        return view('index@Borrows/index',['in_data'=>$new_data,'q_num'=>$q_num,'sq_num'=>$sq_num,'sh_num'=>$sh_num,'dh_num'=>$dh_num]);
    }
    //添加文档到归档库里面
    public function addFile(){
        $ac_data = $this->ac_model->where(array('cate_type'=>1))->select();
        if($ac_data) {
            $cate_tree = recursionCate($ac_data, 'cate_id', 0, 'cate_parent_id');
            $tree = createCateTree($cate_tree, 'cate_id', 'cate_parent_id', 'cate_name');
        }else{
            $tree = '';
        }
        if($this->request->instance()->isPost()){
            $arc_id = $this->request->post('arc_id');
            $bor_time_limit = $this->request->post('bor_time_limit');
            $bor_type = $this->request->post('bor_type');
            $bor_num = $this->request->post('bor_num');
            $bor_desc = $this->request->post('bor_desc');
            if(empty($arc_id)){
                jsonOut(0,'请选择借阅文档');
            }
            if($bor_time_limit < date('Y-m-d')){
                jsonOut(0,'期限不能小于当前时间');
            }
            if(empty($bor_num)){
                jsonOut(0,'借阅份数错误');
            }

            $save_data = array(
                'arc_id'=>$arc_id,
                'bor_time_limit'=>$bor_time_limit.' 23:59:59',
                'bor_type'=>$bor_type,
                'bor_num'=>$bor_num,
                'bor_desc'=>$bor_desc,
                'bor_status'=>1,
                'create_user_id'=>$this->admin_session['id'],
                'create_time'=>date('Y-m-d H:i:s'),
                'update_user_id'=>$this->admin_session['id'],
                'update_time'=>date('Y-m-d H:i:')
            );
            if($this->borrow_model->insert($save_data)){
                $id = $this->borrow_model->getLastInsID();
                $this->tableBak('borrow', 'borrow_version', 'bor_id', 'original_id', $id, 'backup_reasons', '申请借阅');
                jsonOut(1,'申请借阅成功');
            }else{
                jsonOut(0,'申请借阅失败');
            }
        }
        return view('index@Borrows/addFile',['tree'=>$tree,'date'=>date('Y-m-d')]);
    }

    //审核借阅
    public function auditFile(){
        $id = $this->request->get('id');
        if($this->request->instance()->isPost()){
            $id = $this->request->post('id');
            $bor_desc = $this->request->post('bor_desc');
            $dh = $this->request->post('dh');
            if(empty($id)){
                jsonOut(0,'参数错误');
            }
            $data = $this->borrow_model->getDataByCondition(array('bor_id'=>$id));
            if(empty($data)){
                jsonOut(0,'暂无数据');
            }
            if(!empty($dh)){
                if($this->borrow_model->updateByCondition(array('bor_id'=>$id),array('bor_status'=>3,'bor_desc'=>$bor_desc))){
                    $this->tableBak('borrow', 'borrow_version', 'bor_id', 'original_id', $id, 'backup_reasons', '借阅被打回');
                    jsonOut(1,'审核打回成功');
                }else{
                    jsonOut(0,'审核打回失败');
                }
            }
            $save_data = array(
                'bor_status'=>2,
                'bor_desc'=>$bor_desc
            );
            if($this->borrow_model->updateByCondition(array('bor_id'=>$id),$save_data)){
                $this->tableBak('borrow', 'borrow_version', 'bor_id', 'original_id', $id, 'backup_reasons', '借阅审核成功');
                jsonOut(1,'审核成功');
            }else{
                jsonOut(0,'审核失败');
            }
        }
        if(empty($id)){
            jsonOut(0,'参数错误');
        }
        $data = $this->borrow_model->getDataByCondition(array('bor_id'=>$id));
        if(empty($data)){
            jsonOut(0,'暂无数据');
        }
        $arc_data = $this->a_model->getDataByCondition(array('arc_id'=>$data['arc_id']));
        if($arc_data){
            $data['arc_name'] = $arc_data['arc_name'];
            $data['arc_type'] = $arc_data['arc_type'];
            $data['arc_file_size'] = $arc_data['arc_file_size'];
        }


        return view('index@Borrows/auditFile',['data'=>$data]);
    }

    //获取指定的 目录结构
    public function getTree(){
        $cate_type = $this->request->post('cate_type');
        if(empty($cate_type)){
            jsonOut(0,'参数错误');
        }
        $ac_data = $this->ac_model->where(array('cate_type'=>$cate_type))->select();
        if($ac_data) {
            $cate_tree = recursionCate($ac_data, 'cate_id', 0, 'cate_parent_id');
            $tree = createCateTree($cate_tree, 'cate_id', 'cate_parent_id', 'cate_name');
            jsonOut(1,'查询成功',$tree);
        }else{
            jsonOut(0,'暂无数据');
        }
    }

    //借阅后的显示
    public function showFile(){
        $id = $this->request->get('id');
        if(empty($id)){
            jsonOut(0,'参数错误');
        }
        $data = $this->borrow_model->getDataByCondition(array('bor_id'=>$id));
        if(empty($data)){
            jsonOut(0,'暂无数据');
        }
        $arc_data = $this->a_model->getDataByCondition(array('arc_id'=>$data['arc_id']));
        if($arc_data){
            $data['arc_name'] = $arc_data['arc_name'];
            $data['arc_type'] = $arc_data['arc_type'];
            $data['arc_file_size'] = $arc_data['arc_file_size'];
        }
        return view('index@Borrows/showFile',['data'=>$data]);
    }
 
}
